package logic

import (
	"context"
	"fmt"
	"strings"
	"yuni/model/mysql"

	"yuni/yunirpc/internal/svc"
	"yuni/yunirpc/yunirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type EvaluateAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewEvaluateAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EvaluateAddLogic {
	return &EvaluateAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *EvaluateAddLogic) EvaluateAdd(in *yunirpc.EvaluateAddRequest) (*yunirpc.EvaluateAddResponse, error) {
	// todo: add your logic here and delete this line

	var m mysql.Product
	mysql.Db.Where("id=?", in.ProductId).Find(&m)
	if m.ID == 0 {
		return nil, fmt.Errorf("商品不存在")
	}
	evaluate := mysql.Evaluate{
		ProductId: in.ProductId,
		Content:   in.Content,
	}
	if strings.Contains(in.Content, "欺诈") || strings.Contains(in.Content, "伪劣") {
		return nil, fmt.Errorf("检测评价内容中的敏感词如欺诈、伪劣")
	}
	mysql.Db.Debug().Create(&evaluate)

	return &yunirpc.EvaluateAddResponse{
		EvaluateId: int64(evaluate.ID),
	}, nil
}
